package io.xccit.race.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import io.xccit.common.annotation.Log;
import io.xccit.common.core.controller.BaseController;
import io.xccit.common.core.domain.AjaxResult;
import io.xccit.common.enums.BusinessType;
import io.xccit.race.domain.RaceCompetitionTopic;
import io.xccit.race.service.IRaceCompetitionTopicService;
import io.xccit.common.utils.poi.ExcelUtil;
import io.xccit.common.core.page.TableDataInfo;
import io.xccit.race.service.MinioService;

/**
 * 竞赛试题Controller
 * 
 * @author ywx
 * @date 2025-10-10
 */
@RestController
@RequestMapping("/race/competitionTopic")
public class RaceCompetitionTopicController extends BaseController
{
    @Autowired
    private IRaceCompetitionTopicService raceCompetitionTopicService;

    @Autowired
    private MinioService minioService;

    /**
     * 查询竞赛试题列表
     */
    @PreAuthorize("@ss.hasPermi('race:competitionTopic:list')")
    @GetMapping("/list")
    public TableDataInfo list(RaceCompetitionTopic raceCompetitionTopic)
    {
        startPage();
        List<RaceCompetitionTopic> list = raceCompetitionTopicService.selectRaceCompetitionTopicList(raceCompetitionTopic);
        return getDataTable(list);
    }

    /**
     * 根据竞赛ID查询试题列表
     */
    @PreAuthorize("@ss.hasPermi('race:competitionTopic:list')")
    @GetMapping("/listByCompetition/{competitionId}")
    public AjaxResult listByCompetition(@PathVariable("competitionId") Long competitionId)
    {
        List<RaceCompetitionTopic> list = raceCompetitionTopicService.selectRaceCompetitionTopicByCompetitionId(competitionId);
        return AjaxResult.success(list);
    }

    /**
     * 导出竞赛试题列表
     */
    @PreAuthorize("@ss.hasPermi('race:competitionTopic:export')")
    @Log(title = "竞赛试题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RaceCompetitionTopic raceCompetitionTopic)
    {
        List<RaceCompetitionTopic> list = raceCompetitionTopicService.selectRaceCompetitionTopicList(raceCompetitionTopic);
        ExcelUtil<RaceCompetitionTopic> util = new ExcelUtil<RaceCompetitionTopic>(RaceCompetitionTopic.class);
        util.exportExcel(response, list, "竞赛试题数据");
    }

    /**
     * 获取竞赛试题详细信息
     */
    @PreAuthorize("@ss.hasPermi('race:competitionTopic:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(raceCompetitionTopicService.selectRaceCompetitionTopicById(id));
    }

    /**
     * 新增竞赛试题
     */
    @PreAuthorize("@ss.hasPermi('race:competitionTopic:add')")
    @Log(title = "竞赛试题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RaceCompetitionTopic raceCompetitionTopic)
    {
        return toAjax(raceCompetitionTopicService.insertRaceCompetitionTopic(raceCompetitionTopic));
    }

    /**
     * 上传竞赛试题文件
     */
    @PreAuthorize("@ss.hasPermi('race:competitionTopic:add')")
    @Log(title = "竞赛试题", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public AjaxResult uploadTopic(MultipartFile file, Long competitionId) throws Exception
    {
        if (file == null || file.isEmpty()) {
            return AjaxResult.error("请选择要上传的文件");
        }
        
        // 检查文件类型
        String fileName = file.getOriginalFilename();
        String fileType = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
        if (!"pdf".equals(fileType) && !"docx".equals(fileType) && !"doc".equals(fileType)) {
            return AjaxResult.error("只支持上传PDF、DOCX、DOC格式的文件");
        }
        
        // 上传到MinIO
        String url = minioService.upload(file);
        
        // 保存到数据库
        RaceCompetitionTopic raceCompetitionTopic = new RaceCompetitionTopic();
        raceCompetitionTopic.setCompetitionId(competitionId);
        raceCompetitionTopic.setUrl(url);
        raceCompetitionTopic.setFileName(fileName);
        raceCompetitionTopic.setFileSize(file.getSize());
        raceCompetitionTopic.setFileType(fileType);
        
        int result = raceCompetitionTopicService.insertRaceCompetitionTopic(raceCompetitionTopic);
        if (result > 0) {
            return AjaxResult.success("上传成功", raceCompetitionTopic);
        } else {
            return AjaxResult.error("上传失败");
        }
    }

    /**
     * 修改竞赛试题
     */
    @PreAuthorize("@ss.hasPermi('race:competitionTopic:edit')")
    @Log(title = "竞赛试题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RaceCompetitionTopic raceCompetitionTopic)
    {
        return toAjax(raceCompetitionTopicService.updateRaceCompetitionTopic(raceCompetitionTopic));
    }

    /**
     * 删除竞赛试题
     */
    @PreAuthorize("@ss.hasPermi('race:competitionTopic:remove')")
    @Log(title = "竞赛试题", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(raceCompetitionTopicService.deleteRaceCompetitionTopicByIds(ids));
    }
}
